﻿using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPFStudy.ViewModel;

namespace WPFStudy.View
{
    /// <summary>
    /// MvvmLightWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MvvmLightWindow : Window
    {
        public MvvmLightWindow()
        {
            InitializeComponent();
            this.DataContext = new MvvmLightWindowVM();
        }

        string _messageToken = "";
        private void btnRegisterMessenger_Click(object sender, RoutedEventArgs e)
        {
            _messageToken = new Guid().ToString();
            Messenger.Default.Register<string>(this, _messageToken, new Action<string>(p =>
            {
                MessageBox.Show($"接收到发送的消息，消息内容为：\r\n{p}", "Messenger", MessageBoxButton.OK, MessageBoxImage.Information);
            }));
        }

        private void btnSendMessenger_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_messageToken))
            {
                MessageBox.Show($"Messenger未注册，请先注册消息","错误",MessageBoxButton.OK,MessageBoxImage.Error);
                return;
            }
            Messenger.Default.Send<string>($"消息发送时间：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}", _messageToken);

        }

        private void btnUnRegisterMessenger_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_messageToken))
            {
                MessageBox.Show($"Messenger未注册，请先注册消息", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            Messenger.Default.Unregister<string>(this, _messageToken);
            _messageToken = "";
            MessageBox.Show($"Messenger已取消注册", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}
